package com.zhongyouyun.admin.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.*;
import com.zhongyouyun.admin.mapper.RoleMenuMapper;
import com.zhongyouyun.admin.pojo.RoleMenuPojo;
import com.zhongyouyun.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyouyun.admin.vo.MenuVo;
import com.zhongyouyun.other.model.RoleMenuOrDict;
import com.zhongyouyun.utils.LongUtil;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.TreeUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色资源中间表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuModel> implements RoleMenuService {
    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleDictService roleDictService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<RoleMenuModel> selectPage(Page<RoleMenuModel> page, QueryWrapper<RoleMenuModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<RoleMenuModel> selectList(QueryWrapper<RoleMenuModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public MenuVo findMenuALLTreeList(String id) {

        List<MenuModel> menuModelList = menuService.findMenuALLTreeList();
        if (ObjectUtil.isNotEmpty(id)) {
            RoleModel roleModel = new RoleModel();
            roleModel.setId(Long.valueOf(id));
            //这里可以处理系统管理员和单位的区别
            if ("".equals(roleModel.getId())) {
                return null;
            } else {
                RoleModel model = roleService.getById(id);
                MenuVo menuVo = new MenuVo();
                menuModelList.removeIf(menuModel1 -> menuModel1.getId().equals(1624208700358660098L));//菜单管理
                menuModelList.removeIf(menuModel1 -> menuModel1.getId().equals(1625294376793735169L)); //字典管理
                menuModelList.removeIf(menuModel1 -> menuModel1.getId().equals(1663488130880794626L)); //定时任务
                List<MenuModel> menuModels = TreeUtil.listToTree(menuModelList);
                menuVo.setMenuModelList(menuModels);
                menuVo.setUpdate(true);
                if ("0".equals(model.getType())) {
                    menuVo.setUpdate(false);
                }

                return menuVo;
            }
        }
        return null;
    }


    @Override
    public List<MenuModel> selectRoleMenuList(String id) {
        if (ObjectUtil.isEmpty(id))
            return null;
        {
            RoleModel roleModel = new RoleModel();
            roleModel.setId(Long.valueOf(id));
            //这里可以处理系统管理员和单位的区别
            if ("".equals(roleModel.getId())) {
                return null;
            } else {
                List<MenuModel> vmenuModelList = this.baseMapper.selectMenuList(roleModel);
                return vmenuModelList;
            }
        }
    }

    @Override
    public List<MenuModel> findPermissions(String id) {
        if ("510".equals(id)) {
            QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.orderByAsc("order_num");
            List<MenuModel> menuModelList = menuService.list(queryWrapper);//到时直接换成admin管理的
            List<MenuModel> list = com.zhongyouyun.utils.TreeUtil.listToTree(menuModelList);
            return list;
        } else {
            UserModel userModel = new UserModel();
            userModel.setId(Long.valueOf(id));
            List<MenuModel> menuModelList = this.baseMapper.findPermissions(userModel);
            List<MenuModel> list = com.zhongyouyun.utils.TreeUtil.listToTree(menuModelList);
            return list;
        }
    }

    @Transactional
    public Boolean saveOrUpdateforRoleMenuModel(List<RoleMenuOrDict> roleMenuOrDictList,String type) {
        Boolean flag = false;
        Long roleId = roleMenuOrDictList.get(0).getRoleId();
        //清楚权限
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", roleId);
        this.baseMapper.delete(queryWrapper);
        roleDictService.getBaseMapper().delete(queryWrapper);
        //保存权限
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        List<RoleMenuModel> roleMenuModelList = new ArrayList<RoleMenuModel>();
        List<RoleDictModel> roleDictModelList = new ArrayList<RoleDictModel>();
        ModelUtils modelutils = new ModelUtils();
        roleMenuOrDictList.forEach(roleMenuOrDict -> {
            RoleMenuModel roleModel = new RoleMenuModel();
            BeanUtil.copyProperties(roleMenuOrDict, roleModel);
            modelutils.initModel(roleModel, model.getId().toString());
            roleModel.setXiangMuType(type);
            roleMenuModelList.add(roleModel);
        });
        flag = saveBatch(roleMenuModelList);
        return flag;
    }
}
